package com.whmall.service.crm.module.supplierchemical.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.whmall.common.enumeration.YesNoEnum;
import com.whmall.component.entity.dto.OperatorReqDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.*;
import com.whmall.service.crm.module.supplierchemical.dao.SupplierChemicalCraftAttachmentMapper;
import com.whmall.service.crm.module.supplierchemical.entity.SupplierChemicalCraftAttachmentDO;
import com.whmall.service.crm.module.supplierchemical.entity.SupplierChemicalCraftDO;
import com.whmall.service.crm.module.supplierchemical.service.SupplierChemicalCraftAttachmentService;
import com.whmall.service.crm.module.supplierchemical.service.SupplierChemicalCraftService;
import com.whmall.service.crm.module.supplierchemical.dao.SupplierChemicalCraftMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SupplierChemicalCraftServiceImpl implements SupplierChemicalCraftService{

    @Autowired
    private SupplierChemicalCraftMapper mapper;
    @Autowired
    private SupplierChemicalCraftAttachmentService attachmentService;
    @Autowired
    private SupplierChemicalCraftAttachmentMapper attachmentMapper;

    @Transactional
    @Override
    public void save(SupplierChemicalCraftAddReqDTO reqDTO, OperatorReqDTO operatorReqDTO) {

        Assert.isTrue(StringUtils.hasText(reqDTO.getMainstreamProcess()) ||
                        StringUtils.hasText(reqDTO.getPackageMaterial()) ||
                        StringUtils.hasText(reqDTO.getReactionType()) ||
                        CollUtil.isNotEmpty(reqDTO.getAttachmentList()),
                "请至少选填一项内容");

        // 保存工艺信息
        SupplierChemicalCraftDO entity = SupplierChemicalCraftDO
                .builder()
                .chemicalId(reqDTO.getChemicalId())
                .companyId(reqDTO.getCompanyId())
                .mainstreamProcess(reqDTO.getMainstreamProcess())
                .packageMaterial(reqDTO.getPackageMaterial())
                .reactionType(reqDTO.getReactionType())
                .createId(operatorReqDTO.getOperatorId())
                .createTime(new Date())
                .build();
        mapper.insertSelective(entity);

        // 保存工艺对应附件信息
        if(CollUtil.isNotEmpty(reqDTO.getAttachmentList())){
            attachmentService.saveBatch(reqDTO.getAttachmentList(),entity.getId(),operatorReqDTO);
        }

    }

    @Transactional
    @Override
    public void removeById(Long id, OperatorReqDTO operatorReqDTO) {
        SupplierChemicalCraftDO entity = mapper.selectById(id);
        Assert.isTrue(null != entity,"请求参数异常");

        SupplierChemicalCraftDO updateEntity = SupplierChemicalCraftDO
                .builder()
                .id(id)
                .isDeleted(YesNoEnum.YES.getCode())
                .updateId(operatorReqDTO.getOperatorId())
                .updateTime(new Date())
                .build();

        mapper.updateByIdSelective(updateEntity);


        List<SupplierChemicalCraftAttachmentDO> attachmentList = attachmentMapper.listByCraftId(id);

        if(CollUtil.isNotEmpty(attachmentList)){
            // 删除对应的附件
            attachmentService.removeBatch(attachmentList.stream()
                    .map(SupplierChemicalCraftAttachmentDO::getId)
                    .collect(Collectors.toList()), operatorReqDTO);
        }

    }

    @Transactional
    @Override
    public void update(SupplierChemicalCraftUpdateDTO reqDTO, OperatorReqDTO operatorReqDTO) {

        Assert.isTrue(StringUtils.hasText(reqDTO.getMainstreamProcess()) ||
                        StringUtils.hasText(reqDTO.getPackageMaterial()) ||
                        StringUtils.hasText(reqDTO.getReactionType()) ||
                        CollUtil.isNotEmpty(reqDTO.getAttachmentList()),
                "请至少选填一项内容");

        SupplierChemicalCraftDO entity = mapper.selectById(reqDTO.getId());
        Assert.isTrue(null != entity,"请求参数异常");

        // 保存工艺信息
        SupplierChemicalCraftDO updateEntity = SupplierChemicalCraftDO
                .builder()
                .id(reqDTO.getId())
                .mainstreamProcess(reqDTO.getMainstreamProcess())
                .packageMaterial(reqDTO.getPackageMaterial())
                .reactionType(reqDTO.getReactionType())
                .updateId(operatorReqDTO.getOperatorId())
                .updateTime(new Date())
                .build();
        mapper.updateByIdSelective(updateEntity);

        List<SupplierChemicalCraftAttachmentDO> attachmentList = attachmentMapper.listByCraftId(entity.getId());

        // 无保存附件 附件信息全部删除
        if(CollUtil.isEmpty(reqDTO.getAttachmentList())){
            attachmentService.removeBatch(attachmentList.stream()
                    .map(SupplierChemicalCraftAttachmentDO::getId)
                    .collect(Collectors.toList()), operatorReqDTO);
            return;
        }

        // 无有效历史附件 附件信息全部保存
        if(CollUtil.isEmpty(attachmentList)){
            attachmentService.saveBatch(reqDTO.getAttachmentList().stream()
                    .map(SupplierChemicalCraftAttachmentReqDTO::getAttachmentUrl)
                    .collect(Collectors.toList()), entity.getId(),operatorReqDTO);
            return;
        }

        // 部分删除附件
        List<Long> idList = attachmentList.stream()
                .map(SupplierChemicalCraftAttachmentDO::getId)
                .filter(e -> reqDTO.getAttachmentList().stream().noneMatch(r -> r.getId().equals(e)))
                .collect(Collectors.toList());
        if(CollUtil.isNotEmpty(idList)){
            attachmentService.removeBatch(idList, operatorReqDTO);
        }


        // 部分新增附件
        List<String> attachments = reqDTO.getAttachmentList().stream()
                .filter(e -> attachmentList.stream()
                        .noneMatch(r -> r.getId().equals(e.getId())))
                .map(SupplierChemicalCraftAttachmentReqDTO::getAttachmentUrl)
                .collect(Collectors.toList());
        if(CollUtil.isNotEmpty(attachments)){
            attachmentService.saveBatch(attachments,entity.getId(),operatorReqDTO);
        }

    }

    @Override
    public List<SupplierChemicalCraftRspDTO> listBy(SupplierChemicalCraftQueryDTO query) {

        List<SupplierChemicalCraftDO> list = mapper.listBy(query);
        if(CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        return list.stream().map(l -> {
            SupplierChemicalCraftRspDTO rspDTO = new SupplierChemicalCraftRspDTO();
            BeanUtils.copyProperties(l,rspDTO);
            return rspDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SupplierChemicalCraftAttachmentRspDTO> listAttachmentByCraftId(Long craftId) {

        List<SupplierChemicalCraftAttachmentDO> list = attachmentMapper.listByCraftId(craftId);

        if(CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }

        return list.stream().map(l -> {
            SupplierChemicalCraftAttachmentRspDTO rspDTO = new SupplierChemicalCraftAttachmentRspDTO();
            BeanUtils.copyProperties(l,rspDTO);
            return rspDTO;
        }).collect(Collectors.toList());
    }
}